<?php
//date range
$date_range = BUtils::GetDateRangeString(strtotime($rows[0]['start_date']), strtotime($rows[0]['end_date']));

$stats_all = array();
$i = $j = $reach = $clicks = $notifications = $spread = $redeems = 0;
foreach ($rows as $k => $r) {
	$stats_all['local_date'][] = date('d.m', $r['ts_local_date']);
	$stats_all['clicks'][] = $clicks += $r['clicks'];
	$stats_all['notifications'][] = $notifications += $r['notifications'];
	$stats_all['spread'][] = $spread += $r['spread'];
	$stats_all['redeems'][] = $redeems += $r['redeems'];
	$stats_all['reach'][] = $reach += $r['reach'];
}

//skip some steps if the steps are too many.
$stats = array();
$steps = count($rows);
$skip = 1;
if ($steps > 21) $skip = floor($steps/7);
for ($i=0; $i<$steps; $i+=$skip) {
	$stats['local_date'][] = $stats_all['local_date'][$i];
	$stats['reach'][] = $stats_all['reach'][$i];
	$stats['clicks'][] = $stats_all['clicks'][$i];
	$stats['notifications'][] = $stats_all['notifications'][$i];
	$stats['spread'][] = $stats_all['spread'][$i];
	$stats['redeems'][] = $stats_all['redeems'][$i];
}
unset($stats_all);
?>
<div id="chart-daily" style="width:640px; height:480px; margin:0 auto"></div>
<script type="text/javascript">
$(function() {
	$('#chart-daily').highcharts({
		chart: {
			type: 'line',
			marginRight: 130,
            marginBottom: 25
		},
		title: {
			text: '<?php echo BUtils::T('special_report_title_lbl'); ?>',
			x: -20 //center
		},
		subtitle: {
			text: '<?php echo $date_range; ?>',
			x: -20
		},
		xAxis: {
			categories: <?php echo json_encode($stats['local_date'], true); ?>,
			type: 'datetime',
			tickInterval: 1
		},
		yAxis: {
			min: -5,
			title: {
				text: '<?php echo BUtils::T('special_report_yaxis_lbl'); ?>'
			},
			allowDecimals: false,
			plotLines: [{
				value: 10,
				width: 2,
				color: '#00aaee'
			}]
		},
		tooltip: {
			valueSuffix: ''
		},
		legend: {
			layout: 'vertical',
			align: 'right',
			verticalAlign: 'top',
			x: -10,
			y: 100,
			borderWidth: 0
		},
		series: [{
			name: '<?php echo BUtils::T('special_report_reach_lbl'); ?>',
			data: <?php echo json_encode($stats['reach'], true); ?>
		}, {
			name: '<?php echo BUtils::T('special_report_clicks_lbl'); ?>',
			data: <?php echo json_encode($stats['clicks'], true); ?>
		}, {
			name: '<?php echo BUtils::T('special_report_notifications_lbl'); ?>',
			data: <?php echo json_encode($stats['notifications'], true); ?>
		}, {
			name: '<?php echo BUtils::T('special_report_redeems_lbl'); ?>',
			data: <?php echo json_encode($stats['redeems'], true); ?>
		}]
	});
});
</script>